package br.com.fidalgotecnologia.seguranca.bean;

import java.io.IOException;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.component.html.HtmlInputText;
import javax.faces.component.html.HtmlInputSecret;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import br.com.fidalgotecnologia.core.util.StringEncryptUtil;
import br.com.fidalgotecnologia.seguranca.controle.CtrlSeguranca;
import br.com.fidalgotecnologia.seguranca.entity.Usuario;


@ManagedBean(name="LoginBean")
public class LoginBean{
	private HtmlInputText login;
	private HtmlInputSecret Senha;
	
	public HtmlInputText getLogin() {
		return login;
	}
	public void setLogin(HtmlInputText login) {
		this.login = login;
	}
	public HtmlInputSecret getSenha() {
		return Senha;
	}
	public void setSenha(HtmlInputSecret senha) {
		Senha = senha;
	}
	
	public String entrar(){
		
		String retorno = null;
		Usuario usuario = new Usuario();
		usuario.setLogin(getLogin().getValue().toString());
		usuario.setSenha(StringEncryptUtil.encrypt(getSenha().getValue().toString()));
		usuario = CtrlSeguranca.getInstance().logarUsuario(usuario);
		if(usuario!=null && usuario instanceof Usuario){
			FacesContext facesContext = FacesContext.getCurrentInstance(); 
			HttpServletRequest request = (HttpServletRequest) facesContext.getCurrentInstance().getExternalContext().getRequest();
			HttpServletResponse response  = (HttpServletResponse) facesContext.getCurrentInstance().getExternalContext().getResponse(); 
			HttpSession session = request.getSession(); 
			session.setAttribute("UsuarioLogado", usuario);
			retorno = "sucesso";
		}
		return retorno;
	}
	
}
